var e = getApp();
const {
  checkQrcode
} = require('../../common/api/send')
const { bindUser } = require('../../common/api/user')
const api = require('../../common/utils/request.js')

Component({
  properties: {
    show: {
      value: false,
      type: Boolean
    },
    QrCode: {
      value: "",
      type: String
    },
    uuid: {
      value: "",
      type: String
    },
    codeId: {
      value: "",
      type: String
    }
  },

  data: {
    timerCheckCode: null
  },
  ready: function () {
    this.checkStatus();
  },
  beforeDestroy() {
    clearTimeout(this.data.timerCheckCode);
  },
  methods: {
    bindUserByuid(dataId){
      var _this = this;
      api.post(bindUser, {
        id: _this.data.codeId,
        wx_uid: dataId
      }).then(res => {
        //成功时回调函数
        console.log(res, '检测状态')
        if (res.errno == 0) {
          _this.triggerEvent("loginSuccess");
          _this.setData({
            show: false
          });
        }else {
          
        }
      }).catch(err => {
        //失败时回调函数
        console.log(err)
      })
    },
    checkStatus() {
      var _this = this;
      api.post(checkQrcode, {
        uuid: this.data.uuid
      }).then(res => {
        //成功时回调函数
        console.log(res, '检测状态')
        if (res.errno == 0) {
          _this.bindUserByuid(res.data);
        }else {
          _this.data.timerCheckCode = setTimeout(() => {
            _this.checkStatus();
          }, 1000);
        }
      }).catch(err => {
        //失败时回调函数
        console.log(err)
      })
    },
    popupSpecsbtn: function (t) {
      console.log(t);
      this.triggerEvent("closeShow");
      this.setData({
        show: false
      });
    },
  }
});